import heapq


class Solution:
    def getKth(self, lo: int, hi: int, k: int) -> int:
        def func(num, memo):
            if num == 1:
                return 1
            if num in memo:
                return memo[num]
            if num % 2 == 0:
                res = func(num // 2, memo) + 1
            else:
                res = func(num*3+1, memo)+1
            memo[num] = res
            return res
        memo = {}
        queue = []
        for i in range(lo, hi+1):
            weight = func(i, memo)
            heapq.heappush(queue, (weight, i))

        for i in range(k):
            res = heapq.heappop(queue)

        return res[1]
